#include "soundSpot.h"

soundSpot::soundSpot() { // by default, the child constructor call the parameterless default constructor (we could force another by doing: soundSpot::soundSpot : LivingSpot (params...) { ..}
 
 // DEFAULT HARDWARE SENDING MODE (per spot):
// per spot:  
 sendSerial=false;
 sendOSC=false;
 sendMIDI=false;

  // DEFAULT SENDING DATA:
 sendingPosition=false;
 sendingColor=false; // in fact, mean intensity of the saccade
 sendingCollision=false;
 sendingTouchWall=false;
 sendingAngle=false; 
 
 // for tests: NOTE: this won't work, because the constructor is called before the setup function that enables the following functions to work!!!!!!!
  //pinMode(31, OUTPUT); digitalWrite(31,HIGH); delay(200); digitalWrite(31, LOW); delay(200); digitalWrite(31,HIGH); delay(200); digitalWrite(31, LOW);   delay(200); digitalWrite(31,HIGH); 
 
}

 void soundSpot::stopAllSending() {
     sendSerial=false;
     sendOSC=false;
     sendMIDI=false;
     sendingPosition=false;
     sendingColor=false;
     sendingCollision=false;
     sendingTouchWall=false;
     sendingAngle=false; 
 }

void soundSpot::sendData() { 
  sendMes.setTopAddress(spotName);
  
  if (sendOSC) {
    
    if (sendingPosition) {
       sendMes.setSubAddress("/pos"); // set address AND subaddress of sending message:
       long    x, y;    //ATTENTION: parameters to setArgs should be long or unsigned long only (not int!!)
       x=(long)getX(); y=(long)getY();
       sendMes.setArgs( "ii", &x, &y);
       osc.sendOsc( &sendMes );
   }
   if (sendingColor) {// note: not an else (we can send different data simultaneously)
        sendMes.setSubAddress("/maxmin"); 
        long maxInt=(long)maxI, minInt=(long)minI;
       sendMes.setArgs( "ii", &maxInt, &minInt);
      osc.sendOsc( &sendMes );
   }
   if (sendingCollision) {// note: not an else (we can send different data simultaneously)
        sendMes.setSubAddress("/bump"); 
       long coll=(long)(collision? 1 : 0);
       sendMes.setArgs( "i", &coll);
      osc.sendOsc( &sendMes );
   }
   if (sendingTouchWall) {// note: not an else (we can send different data simultaneously)
        sendMes.setSubAddress("/border"); 
       long wall=(long)(touchWall? 1 : 0);
       sendMes.setArgs( "i", &wall);
      osc.sendOsc( &sendMes );
   }
    if (sendingAngle) {// note: not an else (we can send different data simultaneously)
        sendMes.setSubAddress("/angle"); 
        float angletosend=target_angle;// + error_angle;
       long ang=(long)( 180.0/PI*((angletosend>0)? angletosend : angletosend+2*PI) ); 
       sendMes.setArgs( "i", &ang);
      osc.sendOsc( &sendMes );
   }
  
  } // end of OSC sending per-spot
  
 if (sendSerial) {    
     Serial.print(int(getX()), DEC);  // rem: getX gives a float between 0 and 4000
     Serial.print('X'); 
     Serial.print(int(getY()), DEC);
     Serial.print('Y'); 
     Serial.print(identifier);
     Serial.print('I'); // for identifier (this will enable setting the coordinates for the proper spot
     // on the processing program).  
  }
  
  if (sendMIDI) {
      //myMidi.midiTouchWall(identifier); //midiCollisionPercussion(i);
  }

}


